import java.util.*;
public class test {
    public static void main(String[] args) {

    }
    //leetcode 771. 宝石与石头
    class Solution1 {
        public int numJewelsInStones(String jewels, String stones) {
            HashSet<Character> set = new HashSet<>();
            char[] arr = jewels.toCharArray();
            int number = 0;
            for(int i = 0;i < arr.length;i++){
                set.add(arr[i]);
            }
            for(int i = 0;i < stones.length();i++){
                char c = stones.charAt(i);
                if(set.contains(c)){
                    number++;
                }
            }
            return number;
        }
    }
    //leetcode 138.随机链表的复制
    class Solution {
        public Node copyRandomList(Node head) {
            HashMap<Node,Node> map = new HashMap<>();
            Node cur = head;
            while(cur != null){
                Node node = new Node(cur.val);
                map.put(cur,node);
                cur = cur.next;
            }
            cur = head;
            while(cur != null){
                map.get(cur).next = map.get(cur.next);
                map.get(cur).random = map.get(cur.random);
                cur = cur.next;
            }
            return map.get(head);
        }
    }
    //leetcode 692.前k个高频单词
    class Solution {
        public List<String> topKFrequent(String[] words, int k) {
            //存储排序后的单词
            List<String> List = new ArrayList<>();
            //HashMap<单词,出现次数>
            HashMap<String,Integer> map = new HashMap<>();
            for(int i = 0;i < words.length;i++){
                String s = words[i];
                //判断该字符串是否已经在哈希表中
                if(!map.containsKey(s)){
                    map.put(s,1);
                }else {
                    map.put(s,map.get(s) + 1);
                }
            }
            //将Map中的结点放入List中(便于使用sort方法排序)
            List<Map.Entry<String,Integer>> list0 = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
            list0.sort(new Comparator<Map.Entry<String, Integer>>() {
                //重写比较方法
                @Override
                public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                    //出现次数的比较(从高到低)
                    int valueCom = o2.getValue().compareTo(o1.getValue());
                    //字典排序(从低到高)
                    int keyCom = o1.getKey().compareTo(o2.getKey());
                    //优先考虑出现次数,若出现次数相同,则考虑字典排序
                    if(valueCom != 0){
                        return valueCom;
                    }
                    return keyCom;
                }
            });
            for(int i = 0;i < map.size() && i < k;i++){
                String s = list0.get(i).getKey();
                List.add(s);
            }
            return List;
        }
    }
}
